import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue";



Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/',
    name: 'login',
    component: () => import('../views/LoginView.vue')

  },
  {
    path:'/home',
    name:'home',
    component: () => import('../views/HomeFirst/HomeView.vue'),
    children: [
      // 子路由（下方内容区域显示）
      { path: '/home', component: () => import('../views/HomeFirst/FirstView.vue'), name: '首页' },

      {path:'/home/user',component: () => import('../views/UserView.vue'),name:'用户信息',
        meta: { requiresAuth: true }
      },

      {
        path: 'system',
        component: () => import('../views/SystemAdmin/UserManageView.vue'),
        name: '系统管理',
        meta: { requiresAuth: true },

        children: [
          { path: '', redirect: 'usermanage' }, // 默认子路由
          { path: 'usermanage', component: () => import('../views/SystemAdmin/UserManage.vue'), name: '用户管理',
            meta: { requiresAuth: true } },
          { path: 'authority', component: () => import('../views/SystemAdmin/AuthorityManage.vue'), name: '角色管理' ,
            meta: { requiresAuth: true }}
        ]
      },

      {path:'/home/deviceadmin',
        component: () => import('../views/Deviceadmin/DeviceadminView.vue'),
        name: '设备管理',
      meta: { requiresAuth: true },
        children:[
         { path: '/home/deviceadmin', redirect: '/home/deviceadmin/devicefile' }, // 默认显示设备基本信息
          { path: '/home/deviceadmin/devicefile', component: () => import('../views/Deviceadmin/DeviceFileView.vue'), name: '设备基本信息' ,
            meta: { requiresAuth: true }},
          { path: '/home/deviceadmin/documents', component: () => import('../views/Deviceadmin/DeviceDocuments.vue'), name: '设备文档' ,
            meta: { requiresAuth: true }},

        ]
      },
      {path:'/home/analysis',
        component: () => import('../views/StatisticAnalysis/StaAnalysis.vue'),
        name: '统计分析',
      meta: { requiresAuth: true },
        children:[
          { path: '/home/analysis', redirect: '/home/analysis/deviceHealthAnalysis' }, // 默认显示设备基本信息
          { path: '/home/analysis/deviceHealthAnalysis', component: () => import('../views/StatisticAnalysis/DeviceAnalysis.vue'), name: '设备健康度分析' ,
            meta: { requiresAuth: true }},
          { path: '/home/analysis/faultDeepAnalysis', component: () => import('../views/StatisticAnalysis/DepthAnalysis.vue'), name: '故障深度分析',
            meta: { requiresAuth: true } },
          { path: '/home/analysis/maintainEffectAnalysis', component: () => import('../views/StatisticAnalysis/EfficiencyAnalysis.vue'), name: '维护效能分析' ,
            meta: { requiresAuth: true }},
          { path: '/home/analysis/energyCostAnalysis', component: () => import('../views/StatisticAnalysis/CostAnalysis.vue'), name: '能效成本分析' ,
            meta: { requiresAuth: true }},
          { path: '/home/analysis/customAnalysis', component: () => import('../views/StatisticAnalysis/CustomAnalysis.vue'), name: '自定义分析配置',
            meta: { requiresAuth: true } },
          {path:'/home/analysis/worktest',component: () => import('../views/StatisticAnalysis/WorkOrder.vue') ,
            meta: { requiresAuth: true }},
        ]
      },

      {path: '/home/datamonitor',component: () => import('../views/DataMonitor/DataMonitor.vue'),
        name: '数据监控',
        redirect: '/home/datamonitor/realtime',
      meta: { requiresAuth: true },
        children:[


          { path: '/home/datamonitor/abnormal',component: () => import('../views/DataMonitor/DataAbnormalDetection.vue'), name: '数据异常检测' ,
            meta: { requiresAuth: true }}, // 默认显示设备基本信息
          {path:'/home/datamonitor/realtime',component: () => import('../views/DataMonitor/RealTimeData.vue'), name: '实时数据' ,
            meta: { requiresAuth: true }},
          {path:'/home/datamonitor/equipmentrepair',component: () => import('../views/DataMonitor/EquipmentRepair.vue') ,
            meta: { requiresAuth: true }},
          {path:'/home/datamonitor/myrepair',component: () => import('../views/DataMonitor/MyRepair.vue') ,
            meta: { requiresAuth: true }},
          {path:'/home/datamonitor/worktest',component: () => import('../views/StatisticAnalysis/WorkOrder.vue') ,
            meta: { requiresAuth: true }},

          {
            path: '/home/datamonitor/visualization',
            name: 'DataVisualization',
          meta: { requiresAuth: true },
            component: () => import('@/views/DataMonitor/DataVisualization.vue')
          }


        ]
      }
    ]
  },


  {
    path: '/register',
    name: 'register',
    component: () => import('../views/RegisterView.vue')
  },


]


// 解决重复导航到同一路由的错误
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    // 忽略 NavigationDuplicated 错误
    if (err.name !== 'NavigationDuplicated') {
      throw err;
    }
  });
};

// 同样处理 replace 方法
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err;
    }
  });
};

const router = new VueRouter({
  routes
})


// 导航守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('satoken') !== null;

  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查是否需要登录
    if (!isAuthenticated) {
      next({ path: '/login' }); // 未登录则重定向到登录页面
    } else {
      next(); // 已登录则继续
    }
  } else {
    next(); // 不需要登录则继续
  }
});

export default router
